package com.ruis.kotboot.common.base

import com.baomidou.kisso.SSOHelper
import com.baomidou.kisso.security.token.SSOToken
import com.baomidou.mybatisplus.plugins.Page
import com.ruis.kotboot.common.core.RuisException
import com.ruis.kotboot.common.util.HttpUtil
import org.springframework.beans.factory.annotation.Autowired
import java.util.*
import javax.servlet.http.HttpServletRequest


/**
 * Created by mgr on 2017/12/28.
 */
abstract class RuisService<T: RuisModel,D:RuisMapper<T>> {

    @Autowired
    protected val mapper:D?=null

    fun finds(params:Map<String, Any?>): List<Long>?{
        return mapper?.finds(params)
    }

    fun findList(et:T): List<T>?{
        return mapper?.findList(et)
    }
    fun findList(params:Map<String, Any?>): List<T>?{
        return mapper?.findList(params)
    }

    fun findOne(params:Map<String, Any?>):T?{
        val pars= hashMapOf<String, Any?>("limit" to 1)
        pars.putAll(params)
        val list=findList(pars)
        if(list?.size!!>0)
            return list[0]
        return null
    }

    fun findOne(et:T):T?{
        val list=findList(et)
        if(list?.size!!>0)
            return list[0]
        return null
    }

    fun findID(id:Long?):T?{
        if(id==null||id<1)return null
        return mapper?.selectById(id)
    }

    fun findPage(params: Map<String, Any?>):Page<T>{
        return getPage(params).also {
            it.records=mapper?.findList(it,params) ?: arrayListOf()
        }
    }
    fun findCount(params: Map<String, Any?>):Int{
        return mapper?.findCount(params)?:0
    }
    fun delList(ids:Array<*>){
        mapper?.delList(ids)
    }

    fun save(entity:T?):Int?{
        return save(entity, HttpUtil.getRequestSync())
    }
    fun save(entity:T?,req:HttpServletRequest?):Int?{
        val token:SSOToken?=if(req!=null)
            SSOHelper.attrToken(req)?:SSOHelper.getSSOToken(req)
        else
            null
        return save(entity,token?.id?.toLong())
    }

    open fun save(entity:T?,ren:Long?):Int?{
        var rens=ren
        if(ren!=null&&ren<=0)rens=null
        if (entity==null)throw RuisException()
        entity.preHandle()
        if(entity.id==null||entity.id!!<=0) {
            entity.createBy=rens
            entity.createDate= Date()
            return mapper?.insert(entity) ?: -1
        }else {
            entity.updateBy=rens
            entity.updateDate= Date()
            return mapper?.updateById(entity) ?: -1
        }
    }


    /** 分页查询  */
    fun getPage(params: Map<String, Any?>): Page<T> {
        var current = 1
        var size = 10
        try {
            if (params.containsKey("pageNum"))
                current = params["pageNum"] as Int
            if (params.containsKey("pageSize"))
                size = params["pageSize"] as Int
        }catch (e:Exception) { }
        if (size < 0) {
            val page = Page<T>()
            page.isAsc = false
            return page
        }
        val page = Page<T>(current, size)
        page.isAsc = false
        return page
    }
}